Page({
  data: {
    typeIndex: '2',
    pickerIndex: '2',
    type: ['车找人', '人找车', '全部'],
    picker: ['合肥到涡阳', '涡阳到合肥', '全部'],
    list:[],
    share:{
      title:'',
      desc:''
    }
  },
  add: function (e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },
  onLoad() {
    this.queryList(this.data.typeIndex, this.data.pickerIndex);
  },
  queryList(type,wayType){
    // 1. 获取数据库引用
    const db = wx.cloud.database();
    if (type != '2' && wayType != '2'){
      db.collection('t_car_data').where({
        type: type,
        way_type: wayType
      }).orderBy('create_time', 'desc').get().then(res => {
        let data = res.data;
        this.setData({
          list: data
        })
      })
      return;
    }
    if (type == '2' && wayType != '2') {
      db.collection('t_car_data').where({
        way_type: wayType
      }).orderBy('create_time', 'desc').get().then(res => {
        let data = res.data;
        this.setData({
          list: data
        })
      })
      return;
    }
    if (type != '2' && wayType == '2') {
      db.collection('t_car_data').where({
        type: type
      }).orderBy('create_time', 'desc').get().then(res => {
        let data = res.data;
        this.setData({
          list: data
        })
      })
      return;
    }
    if (type == '2' && wayType == '2') {
      db.collection('t_car_data').orderBy('create_time', 'desc')
      .get().then(res => {
        let data = res.data;
        this.setData({
          list: data
        })
      })
      return;
    }
  },
  TypeChange(e) {
    this.setData({
      typeIndex: e.detail.value
    })
    this.queryList(this.data.typeIndex, this.data.pickerIndex);
  },
  PickerChange(e) {
    this.setData({
      pickerIndex: e.detail.value
    })
    this.queryList(this.data.typeIndex, this.data.pickerIndex);
  },
  onShareAppMessage: function () {
    return {
      title: this.data.share.title,
      desc: this.data.share.desc,
      path: 'pages/home/index'
    }
  }
  
})